package com.xxchat.redis;

import com.xxchat.entity.constants.RedisConstants;
import com.xxchat.entity.dto.SysSettingDTO;
import com.xxchat.entity.dto.TokenUserInfoDTO;
import io.netty.channel.Channel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component("redisComponent")
public class RedisComponent {

    @Resource
    private RedisUtils redisUtils;

    /**
     * 获取心跳
     * @param userId
     * @return  时间戳
     */
    public Long getUserHeartBeat(String userId) {
        return (Long) redisUtils.get(RedisConstants.REDIS_KEY_WS_USER_HEARTBEAT + userId);
    }

    /**
     * 保存心跳
     * @param userId
     */
    public void saveUserHeartBeat(String userId) {
        redisUtils.setex(RedisConstants.REDIS_KEY_WS_USER_HEARTBEAT + userId, System.currentTimeMillis(), RedisConstants.REDIS_EXPIRE_TIME_HEARTBEAT);
    }

    /**
     * 清除心跳
     * @param userId
     */
    public void cleanUserHeartBeat(String userId) {
        redisUtils.delete(RedisConstants.REDIS_KEY_WS_USER_HEARTBEAT + userId);
    }


    public void saveTokenUserInfoDTO(TokenUserInfoDTO userInfoDTO) {
        // userId 存 token
        redisUtils.setex(RedisConstants.REDIS_KEY_WS_TOKEN_USERID + userInfoDTO.getUserId(), userInfoDTO.getToken(), RedisConstants.REDIS_EXPIRE_TIME_ONE_DAY * 2);
        // token 再存 userInfo
        redisUtils.setex(RedisConstants.REDIS_KEY_WS_TOKEN + userInfoDTO.getToken(), userInfoDTO, RedisConstants.REDIS_EXPIRE_TIME_ONE_DAY * 2);
    }

    public String getTokenByUserId(String userId) {
        return (String) redisUtils.get(RedisConstants.REDIS_KEY_WS_TOKEN_USERID + userId);
    }

    public TokenUserInfoDTO getUserInfoByToken(String token) {
        return (TokenUserInfoDTO) redisUtils.get(RedisConstants.REDIS_KEY_WS_TOKEN + token);
    }

    public TokenUserInfoDTO getUserInfoByUserId(String userId) {
        String token = getTokenByUserId(userId);
        return (TokenUserInfoDTO) redisUtils.get(RedisConstants.REDIS_KEY_WS_TOKEN + token);
    }

    public void cleanTokenByUserId(String userId) {
        String token = this.getTokenByUserId(userId);
        redisUtils.delete(RedisConstants.REDIS_KEY_WS_TOKEN_USERID + userId);
        if (!StringUtils.isEmpty(token)) {
            redisUtils.delete(RedisConstants.REDIS_KEY_WS_TOKEN + token);
        }
    }

    public SysSettingDTO getSysSetting() {
        SysSettingDTO sysSettingDTO = (SysSettingDTO) redisUtils.get(RedisConstants.REDIS_KEY_SYS_SETTING);
        return sysSettingDTO == null ? new SysSettingDTO() : sysSettingDTO;
    }

    public void saveSysSetting(SysSettingDTO sysSettingDTO) {
        redisUtils.set(RedisConstants.REDIS_KEY_SYS_SETTING, sysSettingDTO);
    }

    public void saveUserContactBatch(String userId, List<String> contactIds) {
        redisUtils.lpushAll(RedisConstants.REDIS_KEY_USER_CONCAT + userId, contactIds, RedisConstants.REDIS_EXPIRE_TIME_ONE_DAY * 2);
    }

    public void saveUserContact(String userId, String contactId) {
        List<String> contactList = this.getUserContactList(userId);
        if (contactList.contains(contactId)) {
            return;
        }
        redisUtils.lpush(RedisConstants.REDIS_KEY_USER_CONCAT + userId, contactId, RedisConstants.REDIS_EXPIRE_TIME_ONE_DAY * 2);
    }

    public List<String> getUserContactList(String userId) {
        return (List<String>) redisUtils.getQueueList(RedisConstants.REDIS_KEY_USER_CONCAT + userId);
    }

    public void cleanUserContact(String userId) {
        redisUtils.delete(RedisConstants.REDIS_KEY_USER_CONCAT + userId);
    }

    public void removeUserContact(String userId, String contactId) {
        redisUtils.remove(RedisConstants.REDIS_KEY_USER_CONCAT + userId, contactId);
    }
}
